import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QVBoxLayout
from PyQt5.QtCore import Qt,QFile
import config_json
from ui_todo import Ui_Form
class TodoModel(QtCore.QAbstractListModel):
    def __init__(self, *args, todos=None, **kwargs):
        super(TodoModel, self).__init__(*args, **kwargs)
        self.todos = todos or []
    def data(self, index, role):
        if role == Qt.DisplayRole:
            text = self.todos[index.row()]
            return text
    def rowCount(self, index):
        return len(self.todos)
class Dlgbeizhu(QtWidgets.QDialog):
    ui=Ui_Form()
    def __init__(self, parent=None):
        QtWidgets.QDialog.__init__(self, parent) 
        self.ui.setupUi(self)       
        self.setWindowTitle("备注")
        # loader = QUiLoader()
        # file = QFile("todo.ui")
        # file.open(QFile.ReadOnly)
        # self.ui = loader.load(file, self)
        # file.close()
        # layout = QVBoxLayout()
        # layout.addWidget(self.ui)
        # self.setLayout(layout)
        self.model = TodoModel()
        self.load()
        self.ui.todoView.setModel(self.model)
        self.ui.addButton.pressed.connect(self.add)
        self.ui.deleteButton.pressed.connect(self.delete)
        # self.ui.completeButton.pressed.connect(self.complete)
        self.ui.btn_ok.clicked.connect(self.ok)
        self.ui.btn_cancel.clicked.connect(self.cancel)
    def ok(self):
        config_json.setting["beizhu"]=[]
        for todo in self.model.todos:
            config_json.setting["beizhu"].append(todo)
        # config_json.write_setting()
        self.accept()
    def cancel(self):
        self.reject()
    def add(self):
        """
        Add an item to our todo list, getting the text from the QLineEdit .todoEdit
        and then clearing it.
        """
        text = self.ui.todoEdit.text()
        if text: # Don't add empty strings.
            # Access the list via the model.
            self.model.todos.append( text)
            # Trigger refresh.        
            self.model.layoutChanged.emit()
            # Empty the input
            self.ui.todoEdit.setText("")
            self.save()
        
    def delete(self):
        indexes = self.ui.todoView.selectedIndexes()
        if indexes:
            # Indexes is a list of a single item in single-select mode.
            index = indexes[0]
            # Remove the item and refresh.
            del self.model.todos[index.row()]
            self.model.layoutChanged.emit()
            # Clear the selection (as it is no longer valid).
            self.ui.todoView.clearSelection()
            self.save()
    # def complete(self):
    #     indexes = self.ui.todoView.selectedIndexes()
    #     if indexes:
    #         index = indexes[0]
    #         row = index.row()
    #         status, text = self.model.todos[row]
    #         self.model.todos[row] = text
    #         # .dataChanged takes top-left and bottom right, which are equal 
    #         # for a single selection.
    #         self.model.dataChanged.emit(index, index)
    #         # Clear the selection (as it is no longer valid).
    #         self.ui.todoView.clearSelection()
    #         self.save()
    def load(self):
        print(config_json.setting)
        for one in config_json.setting["beizhu"]:
            self.model.todos.append(one)
    def save(self):
        pass        
if __name__=="__main__":            
    app = QtWidgets.QApplication(sys.argv)
    config_json.read_setting()
    window = Dlgbeizhu()
    window.show()
    app.exec()


